package com.wuhongyu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wuhongyu.common.MyCommon;
import com.wuhongyu.entity.DataSearch;
import com.wuhongyu.entity.Excel;
import com.wuhongyu.entity.ExcelColumnName;
import com.wuhongyu.entity.ExcelData;
import com.wuhongyu.service.ExcelService;
import com.wuhongyu.util.MyUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
public class ExcelController {

	@Autowired
	private ExcelService excelService;

	
	@RequestMapping("/leadingExcel")
	public String leadingExcel() {
		return "leadingExcel";
	}


	/**
	 * 前端添加一个excel表也是整个项目的首页
	 * @param model
	 * @param excel
	 * @return
	 */
	@RequestMapping("/excelList")
	public String excelList(Model model, @Param("excel") MultipartFile excel,String name) {
		//如果前端传来文件,就添加
		if(excel!=null && !"".equals(excel)) {
			Integer newExcelNum = excelService.getExcelNum() + 1;
			excelService.leadingExcel(excel, newExcelNum,name);
		}

		List<Excel> excelList = excelService.findAll();
		model.addAttribute("excelList",excelList);
		return "excelList";
	}

	/**
	 * 修改excel表的名称
	 * @param excel
	 * @return
	 *//*
	@RequestMapping("/updateExcel")
	public String updateExcel(Excel excel){
		if(!StringUtils.isEmpty(excel)){
			excelService.updateExcel(excel);
		}
		return "redirect:/excelList";
	}*/

	/**
	 * 删除一个excel表
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteExcel")
	public String deleteExcel(Integer id){
		excelService.deleteExcel(id);
		return "redirect:/excelList";
	}



	@RequestMapping("/dataList")
	public String datalList(Model model,@Param("currentPage")String currentPage
			,@Param("ExcelData")ExcelData excelData) {

		//如果前端没有选择操作哪个excel表,那么就给他最后一个表
		if(StringUtils.isEmpty(excelData.getExcelnum())) {
			excelData.setExcelnum(excelService.getExcelNum());
		}

		//将对应excelNum的
		//excel_data的数据和excel_column_name的数据传到前端
		List<ExcelColumnName> nameList = excelService.selecColumnNameListByEn(excelData.getExcelnum());
		PageHelper.startPage(MyUtil.getCurrentPage(currentPage),MyCommon.PAGESIZE);
		List<ExcelData> dataList = excelService.selectDataList(excelData);
		PageInfo<ExcelData> page = new PageInfo<>(dataList);
		model.addAttribute("nameList", nameList);
		model.addAttribute("dataList", dataList);
		model.addAttribute("page", page);
		model.addAttribute("dataSearch", excelData);
		return "dataList";
	}


	/**
	 *清空数据库中所有的表
	 * @return
	 */
	@RequestMapping("/cleanAll")
	public String cleanAll() {
		excelService.cleanAll();
		return "leadingExcel";
	}


	/**
	 * 删除一行
	 * @param model
	 * @param excelData  用来分页的
	 * @param currentPage 同上
	 * @return
	 */
	@RequestMapping("/deleteOne")
	public String deleteOne(Model model,@Param("ExcelData")ExcelData excelData,@Param("currentPage")String currentPage) {
		excelService.deleteDataById(excelData.getId());
		return this.datalList(model, currentPage, excelData);
	}

	/**
	 * 删除多行
	 * @param model
	 * @param checkId
	 * @param excelData
	 * @param currentPage
	 * @return
	 */
	@RequestMapping("/deleteBath")
	public String deleteBath(Model model,@Param("checkId") Integer[] checkId,@Param("ExcelData")ExcelData excelData,@Param("currentPage")String currentPage) {
		excelService.deleteByIds(checkId);
		return this.datalList(model, currentPage, excelData);
	}

	/**
	 * 新建行或者修改行之前的查询
	 * @param model
	 * @param excelData
	 * @param currentPage
	 * @return
	 */
	@RequestMapping("/queryBeforeUpdateOrInsert")
	public String queryBeforeUpdateOrInsert(Model model, @Param("ExcelData")ExcelData excelData,@Param("currentPage")String currentPage) {
		//将excleNum对应的excelColumnName查出来
		List<ExcelColumnName> nameList = excelService.selecColumnNameListByEn(excelData.getExcelnum());
		model.addAttribute("nameList", nameList);
		//如果是修改的话,就进行数据回显,和分页
		if(excelData.getId()!=null && !"".equals(excelData.getId())) {
			ExcelData data = excelService.queryById(excelData.getId());
			model.addAttribute("data", data);
			model.addAttribute("dataSearch", excelData);
			model.addAttribute("currentPage", currentPage);
			return "update";
		}
		return "add";
	}

	/**
	 * 修改或者增加一行
	 * @param model
	 * @param dataSearch
	 * @param excelData
	 * @param currentPage
	 * @return
	 */
	@RequestMapping("/updateOrInsertData")
	public String updateOrInsertData(Model model, @Param("DataSearch")DataSearch dataSearch,@Param("ExcelData")ExcelData excelData,@Param("currentPage")String currentPage) {
		excelService.updateOrInser(excelData);
		ExcelData likeData = new ExcelData();
		likeData.setColumn1(dataSearch.getLikecolumn1());
		likeData.setColumn2(dataSearch.getLikecolumn2());
		likeData.setColumn3(dataSearch.getLikecolumn3());
		likeData.setExcelnum(excelData.getExcelnum());
		return this.datalList(model,currentPage, likeData);
	}

	/**
	 * 对列操作之前的查询
	 * @param model
	 * @param excelnum
	 * @param status
	 * @return
	 */
	@RequestMapping("/queryBreforeUpdateColumn")
	public String queryBreforeUpdateColumn(Model model,@Param("excelnum")Integer excelnum, @Param("status")String status) {
		List<ExcelColumnName> nameList = excelService.selecColumnNameListByEn(excelnum);
		model.addAttribute("nameList", nameList);
		model.addAttribute("status", status);
		return "updateColumn";
		
	}
	
	@RequestMapping("/deleteOrInsertColumn")
	public String deleteOrInsertColumn(Model model,@Param("ExcelColumnName")ExcelColumnName excelColumnName,
			@Param("status")String status) {
		//如果状态值(status)为1的话则为删除
		if("1".equals(status)) {
			excelService.deleteColumn(excelColumnName.getId(),excelColumnName.getExcelnum());
		//如果状态值为2的话,则执行插入
		}else if("2".equals(status)) {
			excelService.insertColumn(excelColumnName);
		}
		return this.datalList(model,"1", new ExcelData());
	}
}
